package com.pxy.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
    private TextView mTv1;
    private TextView mTv2;
    private TextView mTv3;
    public static final String TAG = "PXY";

    float lastX;
    float lastY;
    float thisX;
    float thisY;
    long lastDownTime;
    long thisEventTime;
    long longPressTime =5000 ;
    boolean mIsLongPressed = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mTv1 = (TextView) findViewById(R.id.tv1);
        mTv2 = (TextView) findViewById(R.id.tv2);
        mTv3 = (TextView) findViewById(R.id.tv3);
//        mTv3= new MyTextView(this);
        mTv1.setOnClickListener(this);
        mTv2.setOnClickListener(this);
        mTv3.setOnClickListener(this);
//        mTv3.setOnLongClickListener(this);
        mTv1.setOnLongClickListener(this);
        mTv2.setOnLongClickListener(this);
        mTv3.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View view) {
//        Log.e("PXY", "onClick: ");
        switch (view.getId()) {
            case R.id.tv1:
                Log.e("PXY", "onClick  tv1: ");
                break;
            case R.id.tv2:
                Log.e("PXY", "onClick  tv2: ");
                Toast.makeText(this, "tv2我是单机", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv3:
                Log.e("PXY", "onClick  tv3: ");
                Toast.makeText(this, "tv3我是单机", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public boolean onLongClick(View view) {
//        Log.e("PXY", "onLongClick: ");
        switch (view.getId()) {
            case R.id.tv1:
                Log.e("PXY", "onLongClick  tv1: ");
                break;
            case R.id.tv2:
                Toast.makeText(this, "tv2我是长按监听", Toast.LENGTH_SHORT).show();
                Log.e("PXY", "onLongClick  tv2: ");
                break;
            case R.id.tv3:
                Toast.makeText(this, "tv3我是长按监听", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    /**
     * * 判断是否有长按动作发生 * @param lastX 按下时X坐标 * @param lastY 按下时Y坐标 *
     *
     * @param thisX         移动时X坐标 *
     * @param thisY         移动时Y坐标 *
     * @param lastDownTime  按下时间 *
     * @param thisEventTime 移动时间 *
     * @param longPressTime 判断长按时间的阀值
     */
    static boolean isLongPressed(float lastX, float lastY, float thisX,
                                 float thisY, long lastDownTime, long thisEventTime,
                                 long longPressTime) {
        float offsetX = Math.abs(thisX - lastX);
        float offsetY = Math.abs(thisY - lastY);
        long intervalTime = thisEventTime - lastDownTime;
        Log.e("PXY", "offsetX: "+offsetX);
        Log.e("PXY", "offsetY: "+offsetY);
        Log.e("PXY", "intervalTime: "+intervalTime);
        if (offsetX <= 10 && offsetY <= 10 && intervalTime >= longPressTime) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e("PXY", "ACTION_DOWN: ");
                lastX=event.getX();
                lastY=event.getY();
                lastDownTime= event.getDownTime();

                //检测是否长按,在非长按时检测
                if (!mIsLongPressed) {
                    mIsLongPressed = isLongPressed(lastX, lastY, thisX, thisY, lastDownTime, thisEventTime, 5000);
                }
                if (mIsLongPressed) {
                    //长按模式所做的事
                    Log.e(TAG, "onTouchEvent: 我是长按的处理" );
                } else {
                    //移动模式所做的事
                    Log.e(TAG, "onTouchEvent: 我是点击的处理" );
                }
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("PXY", "ACTION_MOVE: ");
                thisX=event.getX();
                thisY=event.getY();
                thisEventTime=event.getEventTime();
                if(!mIsLongPressed){
                    mIsLongPressed = isLongPressed(lastX, lastY, thisX, thisY, lastDownTime, thisEventTime, 5000);
                }

                break;
            case MotionEvent.ACTION_UP:
                Log.e("PXY", "ACTION_UP: ");
                mIsLongPressed=false;
                break;
        }
        return super.onTouchEvent(event);
    }
}
